package io;
import java.sql.*;

public class SqlDb implements DbInterface
{
    private static Connection _conn = null;
    private static String _username = "imusic";
    private static String _password = "asda";
   
    /**
     * There is only a single connection, so it is a sigelton
     * 
     * @return The connection to the db
     */
    public static Connection getConnection()
    {
    	return _conn; 
    }
    
    /**
     * Open a connection to the db
     *
     * @param isFromTau - is the connection established from the database
     */
    public void openConnection(boolean isFromTau, boolean isLocal)
    {
        // loading the driver
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        }
        catch (ClassNotFoundException e) {
            System.out.println("Unable to load the Oracle JDBC driver..");
            java.lang.System.exit(0);
        }
        System.out.println("Driver loaded successfully");
               
        // creating the connection
        System.out.print("Trying to connect.. ");
        try
        {
        	if(isLocal) {
        		_conn = DriverManager.getConnection(
                        "jdbc:oracle:thin:@localhost:1521:XE", _username, _password);
        	}
        	else if(isFromTau)
        		_conn = DriverManager.getConnection(
                        "jdbc:oracle:thin:@orasrv:1521:csodb", _username, _password);
        	else	
        		_conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1555:csodb", _username, _password);
        }
        catch (SQLException e)
        {
            System.out.println("Unable to connect - " + e.toString());
            java.lang.System.exit(0);
        }
        System.out.println("Connected!");
    }
   
    public void closeConnection()
    {
        // closing the connection
        try    {
            _conn.close();
        }
        catch (SQLException e) {
            System.out.println("Unable to close the connection - " + e.toString());
            java.lang.System.exit(0);
        }
    }

    /**
     * Execute a query
     *
     * @param query - The query as a string
     *
     * @post The result will be closed
     *
     * @return The object specifing the result
     */
    public static void executeQuery(String query)
    {
    	Statement stmt;
       
        try {
        	stmt = _conn.createStatement();
            ResultSet result = stmt.executeQuery(query);
            
            stmt.close();
            result.close();
        }
        catch(SQLException e) {
            System.out.println("ERROR executeQuery - " + e.toString());
        }
    }

    /**
     * Update the db using the given query
     *
     * @param stmt - The statement object
     * @param query - The query as a string
     *
     * @return The update's return
     */
    public static int executeUpdate(String query)
    {
    	Statement stmt;
        int result = 0;
       
        try {
        	stmt = _conn.createStatement();
            result = stmt.executeUpdate(query);
            stmt.close();
        }
        catch(SQLException e) {
            System.out.println("ERROR executeUpdate - " + e.toString());
        }
       
        return result;
    }

}
